const middleware1 = (req, res, next) => {
  return next().then(() => {
    console.log('middleware1 end')
  })
}

// const middleware2 = (req, res, next) => {
//   console.log('middleware2 start')
//   next()
// }

const middleware2 = (req, res, next) => {
  return next().then(() => {
    console.log('middleware2 end')
  })
}

const middleware3 = (req, res, next) => {
  // return next()
  console.log('middleware3 end')
}

function run(req, res) {
  let middlewares = [middleware1, middleware2, middleware3]
  const next = () => {
    const middleware = middlewares.shift()
    if (middleware) {
      // 将middleware(req, res, next)包装为Promise对象
      return Promise.resolve(middleware(req, res, next))
    }
  }
  next()
}

run()